package com.pan.basic;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 *
 * @author by panstark
 * @description
 * @notice
 * @date 2021/4/28
 */
@Slf4j
public class StringDemo {

    String myName;

    public static void sbSub(){

        //自己写要这么多代码
        List<String> nameList = new ArrayList<>();
        nameList.add("jone");
        nameList.add("jerry");
        nameList.add("tom");
        StringBuilder sb = new StringBuilder();
        for (String name : nameList) {
            sb.append(name).append(",");
        }
        String nameStr = sb.substring(0,sb.lastIndexOf(","));
        System.out.println(nameStr);

        //采用stringUtils只需要一行代码
        System.out.println(StringUtils.join(nameList, ","));

        Set<String> stringSet = new HashSet<>();
        stringSet.add("qqq");
        stringSet.add("www");
        stringSet.add("eee");
        System.out.println(StringUtils.join(new ArrayList(stringSet), ","));

        //转化set
        System.out.println(String.join(",",stringSet));
        //转化list
        System.out.println(String.join(",",nameList));
        //转化string[]
        System.out.println(String.join(",",stringSet.toArray(new String[0])));

        String join = String.join(",", stringSet.toArray(new String[0]));
        //字符串转list
        System.out.println(Arrays.asList(join.split(",")));
        //字符串转set，list转set
        System.out.println(new HashSet<>(Arrays.asList(join.split(","))));
        //set转list
        System.out.println(new ArrayList(stringSet));
        //注意，这个方法是把set集合当作list的一个元素，相当于List<Set<String>>
        System.out.println(Arrays.asList(stringSet));

        List<String> mytest = new ArrayList<>(Arrays.asList(join.split(",")));
        mytest.add("000");
        System.out.println(mytest);

    }

    public String repalceDemo(String body){
        String what_you_want = body.replaceAll("<body>", "111").replaceAll("</body>","111");

        return what_you_want;
    }


    public static void spliteDemo(String body){
        String[] what_you_want = body.split(",");

        System.out.println(what_you_want[0]);
        System.out.println(what_you_want[0].length());
    }

    /**
     * 单词首字母小写
     * @param str
     * @return
     */
    public static String lowercaseFirstLetter(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        } else {
            return Character.toLowerCase(str.charAt(0)) + str.substring(1);
        }
    }

    /**
     * 我们没有为属性赋值，依然为null
     * 可以看到虚拟机只会为基本属性进行赋值
     * @param args
     */
    public static void main(String[] args) {
//        StringDemo stringDemo = new StringDemo();
//        log.info("myName:{}",stringDemo.myName);
//        String body = "<body>baowen</body>";
//        String s = stringDemo.repalceDemo(body);
//        System.out.println(s);
        String s = "extendPrice".substring(6);
    System.out.println(s);
        spliteDemo("");
    }


}
